
/**
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) xio.name 2006</p>
 * @author xio
 */
function MetaNodeMouseListener(metaNodeModel, wrapper) {
    this.metaNodeModel = metaNodeModel;
    this.wrapper = wrapper;
}
MetaNodeMouseListener.prototype = new MouseListener();
MetaNodeMouseListener.prototype.onMouseDown = function (e) {
    this.moved = false;
    if (e.button != MouseEvent.BUTTON_LEFT) {
        return;
    }
    this.down = true;

    //
    var state = this.wrapper.getStateMonitor().getState();
    switch (state) {
      case StateMonitor.SELECT:
        if (this.metaNodeModel.isEditing()) {
            return;
        }
        var selectedMetaNodeModels = this.wrapper.getModel().getSelectedMetaNodeModels();
        var selectedTransitionModels = this.wrapper.getModel().getSelectedTransitionModels();
        if ((selectedTransitionModels.size() <= 1) && (selectedTransitionModels.size() <= 1) && (!e.ctrlKey)) {
            this.wrapper.getModel().clearSelectedMetaNodeModels();
            this.wrapper.getModel().clearSelectedTransitionModels();
            this.wrapper.getModel().addSelectedMetaNodeModel(this.metaNodeModel);
            //双击节点，可以编辑节点名称；鼠标在节点右下角的点上托拽可以编辑节点尺寸
            this.wrapper.setStatusInfo("双击节点，可以编辑节点名称；鼠标在节点右下角的点上托拽可以编辑节点尺寸");
        }
        break;
      case StateMonitor.TRANSITION:
        var transitionMonitor = this.wrapper.getTransitionMonitor();
        if (this.metaNodeModel.isNewFromAvailable()) {
            transitionMonitor.setFromMetaNodeModel(this.metaNodeModel);
            //可以从 ×× 建立连接
            this.wrapper.setStatusInfo("可以从" + this.metaNodeModel + "建立连接");
        } else {
        	//无法从 ×× 建立连接
            this.wrapper.setStatusInfo("无法从" + this.metaNodeModel + "建立连接");
        }
        break;
    }
};
MetaNodeMouseListener.prototype.onMouseMove = function (e) {
    if (e.button != MouseEvent.BUTTON_LEFT) {
        return;
    }
    if (this.down) {
        this.moved = true;
    }
};
MetaNodeMouseListener.prototype.onMouseOver = function (e) {
    var state = this.wrapper.getStateMonitor().getState();
    switch (state) {
      case StateMonitor.TRANSITION:
        var transitionMonitor = this.wrapper.getTransitionMonitor();
        var fromMetaNodeModel = transitionMonitor.getFromMetaNodeModel();
        if (fromMetaNodeModel) {
            if ((this.metaNodeModel.isNewToAvailable()) && (fromMetaNodeModel != this.metaNodeModel)) {
                transitionMonitor.setToMetaNodeModel(this.metaNodeModel);
            	//可以到 ×× 建立连接
                this.wrapper.setStatusInfo("可以到" + this.metaNodeModel + "建立连接");
            } else {
        		//无法到 ×× 建立连接
                this.wrapper.setStatusInfo("无法到" + this.metaNodeModel + "建立连接");
            }
        }
        break;
    }
};
MetaNodeMouseListener.prototype.onMouseOut = function (e) {
    var state = this.wrapper.getStateMonitor().getState();
    switch (state) {
      case StateMonitor.TRANSITION:
        var transitionMonitor = this.wrapper.getTransitionMonitor();
        if (transitionMonitor.getFromMetaNodeModel() !== null) {
            transitionMonitor.setToMetaNodeModel(null);
        	//未找到连接结束点
            this.wrapper.setStatusInfo("未找到连接结束点。");
        }
        break;
    }
};
MetaNodeMouseListener.prototype.onMouseUp = function (e) {
    this.down = false;
    if (e.button != MouseEvent.BUTTON_LEFT) {
        return;
    }

    //
    var state = this.wrapper.getStateMonitor().getState();
    switch (state) {
      case StateMonitor.SELECT:
        if (this.moved) {
            return;
        }
        if (e.ctrlKey) {
            if (this.metaNodeModel.isSelected()) {
                this.wrapper.getModel().removeSelectedMetaNodeModel(this.metaNodeModel);
            } else {
                this.wrapper.getModel().addSelectedMetaNodeModel(this.metaNodeModel);
            }
            //您正在多选节点，按下键盘的方向键，或者托拽鼠标，可以移动选择节点的位置;按下删除按钮或者按下Delete键盘按键，可以删除选择的元
            this.wrapper.setStatusInfo("您正在多选节点，按下键盘的方向键，或者托拽鼠标，可以移动选择节点的位置;按下删除按钮或者按下Delete键盘按键，可以删除选择的元");
        } else {
            this.wrapper.getModel().clearSelectedMetaNodeModels();
            this.wrapper.getModel().clearSelectedTransitionModels();
            this.wrapper.getModel().addSelectedMetaNodeModel(this.metaNodeModel);
            //双击节点，可以编辑节点名称；鼠标在节点右下角的点上托拽可以编辑节点尺寸
            this.wrapper.setStatusInfo("双击节点，可以编辑节点名称；鼠标在节点右下角的点上托拽可以编辑节点尺寸");
        }
        break;
      case StateMonitor.TRANSITION:
        var transitionMonitor = this.wrapper.getTransitionMonitor();

    	//
        var fromMetaNodeModel = transitionMonitor.getFromMetaNodeModel();
        var toMetaNodeModel = this.metaNodeModel;
        if ((this.metaNodeModel.isNewToAvailable()) && (fromMetaNodeModel) && (fromMetaNodeModel != toMetaNodeModel)) {
            var transitionModel = new TransitionModel(fromMetaNodeModel, toMetaNodeModel);
            this.wrapper.getModel().addTransitionModel(transitionModel);
            //从 ×× 到 ×× 建立连接成功
            this.wrapper.setStatusInfo("从" + fromMetaNodeModel + "到" + toMetaNodeModel + "建立连接成功");
        } else {
            //添加连接失败
            this.wrapper.setStatusInfo("添加连接失败");
        }

    	//
        transitionMonitor.reset();
        break;
    }
};

